import java.util.ArrayList;
import java.util.Queue;

class MyBlockingQueue{
      private  String[] data = new String[100];
      private volatile int head  = 0 ;
      private volatile int end = 0 ;
      private volatile int size = 0 ;

      public void put(String val) throws InterruptedException {

            synchronized(this){
                  while (size == data.length){
                        this.wait();
                  }
                  data[end] = val ;
                  end++ ;
                  size ++ ;
                  if(end == data.length){
                        end = 0;
                  }
                  this.notify();
            }
      }
      public String take() throws InterruptedException {

            synchronized(this){
                  while (size == 0){
                       this.wait();
                  }
                  String tmp = data[head];
                  head++ ;
                  size-- ;
                  if(head == data.length){
                        head = 0 ;
                  }
                  this.notify();
                  return tmp;
            }
      }



}

public class Test5 {

      public static void main(String[] args) {
             MyBlockingQueue myBlockingQueue = new MyBlockingQueue();
            Thread t1 = new Thread(()->{
                  int num = 0 ;
                  while (true){
                      try {
                          myBlockingQueue.put(""+num);
                            System.out.println("put:"+num);
                            num++;
                      } catch (InterruptedException e) {
                          throw new RuntimeException(e);
                      }

                 }
            });
            Thread t2 = new Thread(()->{
                  while (true){
                      try {
                            Thread.sleep(2000);
                          System.out.println("take:"+myBlockingQueue.take());
                      } catch (InterruptedException e) {
                          throw new RuntimeException(e);
                      }
                  }
            });
            t1.start();
            t2.start();

      }
}
